<template>
  <!-- 相关方违规记录 -->
  <div class="relatedpartyviolationrecord">
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        v-model="form"
        ref="crud"
        @search-change="searchChange"
        @search-reset="searchReset"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
      >

        <!-- 操作列表格自定义 -->
        <template slot="operation" slot-scope="scope">
          <el-button
            type="success"
            size="mini"
            icon="el-icon-plus"
            @click="handleViolation(scope.row)">
            添加违规记录
          </el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click="handleViolationRecord(scope.row)">
            查看历史记录
          </el-button>
        </template>

      </avue-crud>

      <!-- 弹窗：添加违规记录 -->
      <el-dialog
        title="添加违规记录"
        append-to-body
        :visible.sync="addViolationDialog"
        :close-on-click-modal="false"
        :before-close="closeAddViolationDialog"
        width="60%"
        class="add_violation_dialog"
      >
        <avue-form
          ref="updateForm"
          v-model="updateObj"
          :option="updateOption"
          @submit="confirmAddViolation"
        >
          <template slot="menuForm">
            <el-button
              size="small"
              icon="el-icon-close"
              @click="closeAddViolationDialog()">
              取 消
            </el-button>
          </template>
        </avue-form>
      </el-dialog>

      <!-- 弹窗：查看历史记录 -->
      <el-dialog
        title="查看历史记录"
        append-to-body
        :visible.sync="viewViolationRecordDialog"
        :close-on-click-modal="false"
        :before-close="closeViewViolationRecordDialog"
        width="60%"
        class="view_violation_record_dialog"
      >

        <!-- 内容 -->
        <avue-crud
          :option="violationOption"
          :table-loading="violationLoading"
          :data="violationData"
          :page.sync="violationPage"
          ref="violationCrud"
          @search-change="violationSearchChange"
          @search-reset="violationSearchReset"
          @current-change="violationCurrentChange"
          @size-change="violationSizeChange"
          @refresh-change="violationRefreshChange"
        >
        </avue-crud>

        <!-- 按钮 -->
        <div class="view_violation_record_footer">
          <el-button
            type="primary"
            size="small"
            @click="closeViewViolationRecordDialog()">
            确 定
          </el-button>
        </div>

      </el-dialog>

    </basic-container>
  </div>
</template>

<script>
import {
  getList,
  add,
  getRecords
} from "@/api/basedata/interestedpartymange/relatedpartyviolationrecord";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      data: [],
      loading: true,
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        border: true,
        index: false,
        selection: false,
        viewBtn: true,
        refreshBtn: true,
        columnBtn: false,
        dialogClickModal: false,
        addBtn: false,
        menu: false,
        column: [
          {
            label: "评定单位",
            prop: "dwmc",
            search: true,
            align: "center"
          },
          {
            label: "开始日期",
            prop: "startDate",
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            hide: true,
            search: true
          },
          {
            label: "结束日期",
            prop: "endData",
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            hide: true,
            search: true
          },
          {
            label: "累计罚款金额",
            prop: "fkje",
            align: "center"
          },
          {
            label: "最近违规时间",
            prop: "wgrq",
            align: "center"
          },
          {
            label: "累计扣分值",
            prop: "kffz",
            align: "center"
          },
          {
            label: "操作",
            prop: "operation",
            align: "center",
            slot: true
          }
        ]
      },
      // 弹窗：添加违规记录
      addViolationDialog: false,
      selectedPartyId: "",
      updateObj: {},
      // 弹窗：查看历史记录
      viewViolationRecordDialog: false,
      selectedRecordId: "",
      violationQuery: {},
      violationLoading: true,
      violationData: [],
      violationPage: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      violationOption: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 6,
        searchMenuSpan: 4,
        selection: false,
        addBtn: false,
        index: false,
        refreshBtn: false,
        columnBtn: false,
        menu: false,
        column: [
          {
            label: "违规年份",
            prop: "year",
            hide: true,
            type: "select",
            dicData: [],
            search: true
          },
          {
            label: "违规日期",
            prop: "wgrq",
            align: "center"
          },
          {
            label: "违规内容",
            prop: "wgnr",
            align: "center"
          },
          {
            label: "相关责任人",
            prop: "xgzrr",
            align: "center"
          },
          {
            label: "扣分分值",
            prop: "kffz",
            align: "center"
          },
          {
            label: "罚款金额",
            prop: "fkje",
            align: "center"
          },
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    updateOption() {
      return {
        size: "small",
        submitText: "确 定",
        labelWidth: 110,
        mockBtn: false,
        printBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "单位名称",
            prop: "dwmc",
            span: 24,
            disabled: true,
            rules: [{
              required: true,
              message: "请输入单位名称",
              trigger: "blur"
            }]
          },
          {
            label: "违规日期",
            prop: "wgrq",
            span: 12,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            rules: [{
              required: true,
              message: "请选择违规日期",
              trigger: "change"
            }]
          },
          {
            label: "相关责任人",
            prop: "xgzrr",
            span: 12
          },
          {
            label: "违规内容",
            prop: "wgnr",
            span: 24,
            type: "textarea",
            maxlength: 150,
            rules: [{
              required: true,
              message: "请输入违规内容",
              trigger: "blur"
            }]
          },
          {
            label: "扣分分值",
            prop: "kffz",
            span: 12,
            type: "number",
            precision: 2,
            minRows: 1,
            step: 1,
            rules: [{
              required: true,
              message: "请输入扣分分值",
              trigger: "blur"
            }]
          },
          {
            label: "罚款金额",
            prop: "fkje",
            span: 12,
            type: "number",
            precision: 2,
            minRows: 1,
            step: 1,
            rules: [{
              required: true,
              message: "请输入罚款金额",
              trigger: "blur"
            }]
          },
          {
            label: "备注",
            prop: "bz",
            span: 24,
            type: "textarea",
            maxlength: 150
          }
        ]
      }
    }
  },
  methods: {

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 更换页数
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },

    // 更换每页显示数量
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 加载数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },

    /**
     * 弹窗：添加违规记录
    */

    // 打开添加违规记录弹窗
    handleViolation(row) {
      this.selectedPartyId = row.relatedPartyId;
      this.updateObj = this.formatUpdateObj(row);
      this.addViolationDialog = true;
    },

    // 格式化违规记录数据
    formatUpdateObj(row) {
      var data = {
        dwmc: row.dwmc,
        wgrq: "",
        xgzrr: "",
        wgnr: "",
        kffz: "",
        fkje: "",
        bz: ""
      };
      return data;
    },

    // 关闭、取消添加违规记录弹窗
    closeAddViolationDialog() {
      this.selectedPartyId = "";
      this.addViolationDialog = false;
    },

    // 确认添加违规记录
    confirmAddViolation() {
      this.$refs.updateForm.validate((vaild,done)=>{
        if(vaild){
          var row = {
            xgfdw: this.selectedPartyId,
            wgrq: this.updateObj.wgrq,
            xgzrr: this.updateObj.xgzrr,
            wgnr: this.updateObj.wgnr,
            kffz: this.updateObj.kffz,
            fkje: this.updateObj.fkje,
            bz: this.updateObj.bz
          };
          add(row).then(res => {
            if(res.data.success) {
              this.closeAddViolationDialog();
              this.page.currentPage = 1;
              this.onLoad(this.page);
            }
          });
          done();
        }
      })
    },

    /**
     * 弹窗：查看历史记录
    */

    // 打开查看历史记录弹窗
    handleViolationRecord(row) {
      var currentYear = new Date().getFullYear();
      var yearArr = [];
      for(let i = 0 ; i < 5 ; i++) {
        yearArr.push({
          label: currentYear - i,
          value: currentYear - i
        });
      };
      var yearColumn = this.findObject(this.violationOption.column, "year");
      yearColumn.dicData = yearArr;
      this.violationPage.currentPage = 1;
      this.selectedRecordId = row.relatedPartyId;
      this.violationOnLoad(this.violationPage);
      this.viewViolationRecordDialog = true;
    },

    // 搜索条件改变
    violationSearchChange(params, done) {
      this.violationQuery = params;
      this.violationPage.currentPage = 1;
      this.violationOnLoad(this.violationPage, params);
      done();
    },

    // 清空搜索条件
    violationSearchReset() {
      this.violationQuery = {};
      this.violationOnLoad(this.violationPage);
    },

    // 更换页数
    violationCurrentChange(currentPage) {
      this.violationPage.currentPage = currentPage;
    },

    // 更换每页显示数量
    violationSizeChange(pageSize) {
      this.violationPage.pageSize = pageSize;
    },

    // 刷新
    violationRefreshChange() {
      this.violationOnLoad(this.violationPage, this.violationQuery);
    },

    // 加载数据
    violationOnLoad(page, params = {}) {
      this.violationLoading = true;
      this.violationQuery["xgfdw"] = this.selectedRecordId;
      getRecords(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.violationQuery)
      ).then(res => {
        var resData = res.data.data;
        this.violationPage.total = resData.total;
        this.violationData = resData.records;
        this.violationLoading = false;
      });
    },

    // 关闭、取消评定记录弹窗
    closeViewViolationRecordDialog() {
      this.viewViolationRecordDialog = false;
      this.selectedRecordId = "";
    }

  }
};
</script>

<style lang="scss">
.add_violation_dialog {
  .avue-form__menu--center {
    text-align: center !important;
    margin-bottom: 0px;
  }
}

.view_violation_record_footer {
  text-align: center;
  margin-top: 15px;

  .el-button {
    width: 100px;
    height: 35px;
  }
}
</style>
